package cc.rengu.igas.bomp.common.dao.impl;

import cc.rengu.igas.bomp.common.dao.UserInfoMapper;
import cc.rengu.igas.bomp.common.entity.UserInfo;
import cc.rengu.jradp.mods.xml.XmlTree;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户信息表操作接口实现类
 * Created by wangcheng on 2021/7/19 14:18
 */
public class UserInfoMapperImpl implements UserInfoMapper {
    private final RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.MCMP_POOL_NAME);

    public UserInfoMapperImpl() throws Exception {
        if (XmlTree.hasThreadTree()) {
            XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
            String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
            if (null != corporation && !corporation.isEmpty()) {
                String dbPoolCashNode = corporation.trim() + AppConfigConstant.DATABASE_CONF_NODE;
                dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
            }
        }
    }

    @Override
    public int insertUserInfo(UserInfo userInfo) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "insertUserInfo");
        int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, userInfo, null);
        if (0 != iReturnCode) {
            rglog.error("登记用户信息失败,instId:<{}>,userId:<{}>,iReturnCode:<{}>", userInfo.getInstId(), userInfo.getUserId(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public int updateUserInfoById(UserInfo userInfo) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateUserInfoById");
        int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, userInfo, null);
        if (0 != iReturnCode) {
            rglog.error("根据非业务主键更新用户信息失败,instId:<{}>,id:<{}>,iReturnCode:<{}>", userInfo.getInstId(), userInfo.getId(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public int updateUserInfoByPrimaryKey(UserInfo userInfo) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateUserInfoByPrimaryKey");
        int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, userInfo, null);
        if (0 != iReturnCode) {
            rglog.error("根据业务主键更新用户信息失败,instId:<{}>,userId:<{}>,iReturnCode:<{}>", userInfo.getInstId(), userInfo.getUserId(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public UserInfo selectUserInfoByPrimaryKey(String instId, String userId) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectUserInfoByPrimaryKey");
        UserInfo userInfo = new UserInfo();
        userInfo.setInstId(instId);
        userInfo.setUserId(userId);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, userInfo, userInfo);
        if (0 != iReturnCode) {
            rglog.error("根据业务主键查询用户信息失败,instId:<{}>,userId:<{}>,iReturnCode:<{}>", instId, userId, iReturnCode);
            return null;
        }
        return userInfo;
    }

    @Override
    public UserInfo selectUserInfoByCertInfo(String instId, String userType, String userInst, String certType, String certNo) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectUserInfoByCertInfo");
        UserInfo userInfo = new UserInfo();
        userInfo.setInstId(instId);
        userInfo.setUserType(userType);
        userInfo.setUserInst(userInst);
        userInfo.setCertType(certType);
        userInfo.setCertNo(certNo);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, userInfo, userInfo);
        if (0 != iReturnCode) {
            rglog.error("根据证件信息查询用户信息失败,instId:<{}>,userInst:<{}>,userInst:<{}>,certType:<{}>,certNo:<{}>,iReturnCode:<{}>", instId, userType, userInst, certType, certNo, iReturnCode);
            return null;
        }
        return userInfo;
    }

    @Override
    public List<UserInfo> selectUserInfoByPhoneNo(String instId, String userType, String userInst, String phoneNo) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectUserInfoByPhoneNo");
        UserInfo userInfo = new UserInfo();
        userInfo.setInstId(instId);
        userInfo.setUserType(userType);
        userInfo.setUserInst(userInst);
        userInfo.setMobileNo(phoneNo);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, userInfo, UserInfo.class);
        if (objectList.isEmpty()) {
            rglog.warn("根据手机号查询用户信息失败，instId:<{}>,userInst:<{}>,userInst:<{}>,phoneNo:<{}>", instId, userType, userInst, phoneNo);
            return null;
        }
        return objectList.stream().map(item -> (UserInfo) (item)).collect(Collectors.toList());
    }

    @Override
    public UserInfo selectUserInfoByOpenId(String instId, String userType, String openId) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectUserInfoByOpenId");
        UserInfo userInfo = new UserInfo();
        userInfo.setInstId(instId);
        userInfo.setUserType(userType);
        userInfo.setRemark1(openId);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, userInfo, userInfo);
        if (0 != iReturnCode) {
            rglog.error("根据openId查询用户信息失败,instId:<{}>,openId:<{}>,iReturnCode:<{}>", instId, openId, iReturnCode);
            return null;
        }
        return userInfo;
    }

    @Override
    public UserInfo selectUserInfoByLoginId(String instId, String userType, String loginId) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectUserInfoByLoginId");
        UserInfo userInfo = new UserInfo();
        userInfo.setInstId(instId);
        userInfo.setUserType(userType);
        userInfo.setLoginId(loginId);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, userInfo, userInfo);
        if (0 != iReturnCode) {
            rglog.error("根据用户登录账号查询用户信息失败,instId:<{}>,loginId:<{}>,iReturnCode:<{}>", instId, loginId, iReturnCode);
            return null;
        }
        return userInfo;
    }
}
